<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>按受欢迎度提升权重 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/boosting-by-popularity.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/boosting-by-popularity.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/boosting-by-popularity.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/boosting-by-popularity.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="search-in-depth.html">深入搜索</a></span>
»
<span class="breadcrumb-link"><a href="controlling-relevance.html">控制相关度</a></span>
»
<span class="breadcrumb-node">按受欢迎度提升权重</span>
</div>
<div class="navheader">
<span class="prev">
<a href="function-score-query.html">« function_score 查询</a>
</span>
<span class="next">
<a href="function-score-filters.html">过滤集提升权重 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="boosting-by-popularity"></a>按受欢迎度提升权重<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/170_Relevance/45_Popularity.asciidoc">edit</a>
</h2>
</div></div></div>
<p>设想有个网站供用户发布博客并且可以让他们为自己喜欢的博客点赞，我们希望将更受欢迎的博客放在搜索结果列表中相对较上的位置，同时全文搜索的评分仍然作为相关度的主要排序依据，可以简单的通过存储每个博客的点赞数来实现它：</p>
<div class="pre_wrapper lang-json pagebreak-before">
<pre class="programlisting prettyprint lang-json pagebreak-before">PUT /blogposts/post/1
{
  "title":   "About popularity",
  "content": "In this post we will talk about...",
  "votes":   6
}</pre>
</div>
<p>在搜索时，可以将 <code class="literal">function_score</code> 查询与 <code class="literal">field_value_factor</code> 结合使用，即将点赞数与全文相关度评分结合：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">GET /blogposts/post/_search
{
  "query": {
    "function_score": { <a id="CO112-1"></a><i class="conum" data-value="1"></i>
      "query": { <a id="CO112-2"></a><i class="conum" data-value="2"></i>
        "multi_match": {
          "query":    "popularity",
          "fields": [ "title", "content" ]
        }
      },
      "field_value_factor": { <a id="CO112-3"></a><i class="conum" data-value="3"></i>
        "field": "votes" <a id="CO112-4"></a><i class="conum" data-value="4"></i>
      }
    }
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO112-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p><code class="literal">function_score</code> 查询将主查询和函数包括在内。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO112-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p>主查询优先执行。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO112-3"><i class="conum" data-value="3"></i></a></p>
</td>
<td align="left" valign="top">
<p><code class="literal">field_value_factor</code> 函数会被应用到每个与主 <code class="literal">query</code> 匹配的文档。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO112-4"><i class="conum" data-value="4"></i></a></p>
</td>
<td align="left" valign="top">
<p>每个文档的 <code class="literal">votes</code> 字段都 <em>必须</em> 有值供 <code class="literal">function_score</code> 计算。如果 <em>没有</em> 文档的 <code class="literal">votes</code> 字段有值，那么就 <em>必须</em> 使用
<a href="https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-function-score-query.html#function-field-value-factor" class="ulink" target="_top"><code class="literal">missing</code> 属性</a> 提供的默认值来进行评分计算。</p>
</td>
</tr>
</table>
</div>
<p>在前面示例中，每个文档的最终评分 <code class="literal">_score</code> 都做了如下修改：</p>
<pre class="literallayout">new_score = old_score * number_of_votes</pre>

<p>然而这并不会带来出人意料的好结果，全文评分 <code class="literal">_score</code> 通常处于 0 到 10 之间，如下图 <a class="xref" href="boosting-by-popularity.html#img-popularity-linear" title="受欢迎度的线性关系基于 _score 的原始值 2.0">Figure 29, “受欢迎度的线性关系基于 <code class="literal">_score</code> 的原始值 <code class="literal">2.0</code>”</a> 中，有 10 个赞的博客会掩盖掉全文评分，而 0 个赞的博客的评分会被置为 0 。</p>
<div id="img-popularity-linear" class="imageblock">
<div class="content">
<img src="../images/elas_1701.png" alt="Linear popularity based on an original `_score` of `2.0`">
</div>
<div class="title">Figure 29. 受欢迎度的线性关系基于 <code class="literal">_score</code> 的原始值 <code class="literal">2.0</code>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_modifier"></a>modifier<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/170_Relevance/45_Popularity.asciidoc">edit</a>
</h3>
</div></div></div>
<p>一种融入受欢迎度更好方式是用 <code class="literal">modifier</code> 平滑 <code class="literal">votes</code> 的值。换句话说，我们希望最开始的一些赞更重要，但是其重要性会随着数字的增加而降低。 0 个赞与 1 个赞的区别应该比 10 个赞与 11 个赞的区别大很多。</p>
<p>对于上述情况，典型的 <code class="literal">modifier</code> 应用是使用 <code class="literal">log1p</code> 参数值，公式如下：</p>
<pre class="literallayout">new_score = old_score * log(1 + number_of_votes)</pre>

<p><code class="literal">log</code> 对数函数使 <code class="literal">votes</code> 赞字段的评分曲线更平滑，如图 <a class="xref" href="boosting-by-popularity.html#img-popularity-log" title="受欢迎度的对数关系基于 _score 的原始值 2.0">Figure 30, “受欢迎度的对数关系基于 <code class="literal">_score</code> 的原始值 <code class="literal">2.0</code>”</a> ：</p>
<div id="img-popularity-log" class="imageblock">
<div class="content">
<img src="../images/elas_1702.png" alt="Logarithmic popularity based on an original `_score` of `2.0`">
</div>
<div class="title">Figure 30. 受欢迎度的对数关系基于 <code class="literal">_score</code> 的原始值 <code class="literal">2.0</code>
</div>
</div>
<p>带 <code class="literal">modifier</code> 参数的请求如下：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">GET /blogposts/post/_search
{
  "query": {
    "function_score": {
      "query": {
        "multi_match": {
          "query":    "popularity",
          "fields": [ "title", "content" ]
        }
      },
      "field_value_factor": {
        "field":    "votes",
        "modifier": "log1p" <a id="CO113-1"></a><i class="conum" data-value="1"></i>
      }
    }
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO113-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p><code class="literal">modifier</code> 为 <code class="literal">log1p</code> 。</p>
</td>
</tr>
</table>
</div>
<p class="pagebreak-before">修饰语 modifier 的值可以为： <code class="literal">none</code> （默认状态）、 <code class="literal">log</code> 、 <code class="literal">log1p</code> 、 <code class="literal">log2p</code> 、 <code class="literal">ln</code> 、 <code class="literal">ln1p</code> 、 <code class="literal">ln2p</code> 、 <code class="literal">square</code> 、 <code class="literal">sqrt</code> 以及 <code class="literal">reciprocal</code> 。想要了解更多信息请参照：
<a href="https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-function-score-query.html#function-field-value-factor" class="ulink" target="_top"><code class="literal">field_value_factor</code> 文档</a>.</p>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_factor"></a>factor<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/170_Relevance/45_Popularity.asciidoc">edit</a>
</h3>
</div></div></div>
<p>可以通过将 <code class="literal">votes</code> 字段与 <code class="literal">factor</code> 的积来调节受欢迎程度效果的高低：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">GET /blogposts/post/_search
{
  "query": {
    "function_score": {
      "query": {
        "multi_match": {
          "query":    "popularity",
          "fields": [ "title", "content" ]
        }
      },
      "field_value_factor": {
        "field":    "votes",
        "modifier": "log1p",
        "factor":   2 <a id="CO114-1"></a><i class="conum" data-value="1"></i>
      }
    }
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO114-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>双倍效果。</p>
</td>
</tr>
</table>
</div>
<p>添加了 <code class="literal">factor</code> 会使公式变成这样：</p>
<pre class="literallayout">new_score = old_score * log(1 + factor * number_of_votes)</pre>

<p><code class="literal">factor</code> 值大于 <code class="literal">1</code> 会提升效果， <code class="literal">factor</code> 值小于 <code class="literal">1</code> 会降低效果，如图 <a class="xref" href="boosting-by-popularity.html#img-popularity-factor" title="受欢迎度的对数关系基于多个不同因子">Figure 31, “受欢迎度的对数关系基于多个不同因子”</a> 。</p>
<div id="img-popularity-factor" class="imageblock">
<div class="content">
<img src="../images/elas_1703.png" alt="Logarithmic popularity with different factors">
</div>
<div class="title">Figure 31. 受欢迎度的对数关系基于多个不同因子</div>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_boost_mode"></a>boost_mode<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/170_Relevance/45_Popularity.asciidoc">edit</a>
</h3>
</div></div></div>
<p>或许将全文评分与 <code class="literal">field_value_factor</code> 函数值乘积的效果仍然可能太大，我们可以通过参数 <code class="literal">boost_mode</code> 来控制函数与查询评分 <code class="literal">_score</code> 合并后的结果，参数接受的值为：</p>
<div class="variablelist">
<dl class="variablelist">
<dt>
<span class="term">
<code class="literal">multiply</code>
</span>
</dt>
<dd>
评分 <code class="literal">_score</code> 与函数值的积（默认）
</dd>
<dt>
<span class="term">
<code class="literal">sum</code>
</span>
</dt>
<dd>
评分 <code class="literal">_score</code> 与函数值的和
</dd>
<dt>
<span class="term">
<code class="literal">min</code>
</span>
</dt>
<dd>
评分 <code class="literal">_score</code> 与函数值间的较小值
</dd>
<dt>
<span class="term">
<code class="literal">max</code>
</span>
</dt>
<dd>
评分 <code class="literal">_score</code> 与函数值间的较大值
</dd>
<dt>
<span class="term">
<code class="literal">replace</code>
</span>
</dt>
<dd>
函数值替代评分 <code class="literal">_score</code>
</dd>
</dl>
</div>
<p>与使用乘积的方式相比，使用评分 <code class="literal">_score</code> 与函数值求和的方式可以弱化最终效果，特别是使用一个较小 <code class="literal">factor</code> 因子时：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">GET /blogposts/post/_search
{
  "query": {
    "function_score": {
      "query": {
        "multi_match": {
          "query":    "popularity",
          "fields": [ "title", "content" ]
        }
      },
      "field_value_factor": {
        "field":    "votes",
        "modifier": "log1p",
        "factor":   0.1
      },
      "boost_mode": "sum" <a id="CO115-1"></a><i class="conum" data-value="1"></i>
    }
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO115-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>将函数计算结果值累加到评分 <code class="literal">_score</code> 。</p>
</td>
</tr>
</table>
</div>
<p>之前请求的公式现在变成下面这样（参见 <a class="xref" href="boosting-by-popularity.html#img-popularity-sum" title="使用 sum 结合受欢迎程度">Figure 32, “使用 <code class="literal">sum</code> 结合受欢迎程度”</a> ）：</p>
<pre class="literallayout">new_score = old_score + log(1 + 0.1 * number_of_votes)</pre>

<div id="img-popularity-sum" class="imageblock">
<div class="content">
<img src="../images/elas_1704.png" alt="Combining popularity with `sum`">
</div>
<div class="title">Figure 32. 使用 <code class="literal">sum</code> 结合受欢迎程度</div>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_max_boost"></a>max_boost<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/170_Relevance/45_Popularity.asciidoc">edit</a>
</h3>
</div></div></div>
<p>最后，可以使用 <code class="literal">max_boost</code> 参数限制一个函数的最大效果：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">GET /blogposts/post/_search
{
  "query": {
    "function_score": {
      "query": {
        "multi_match": {
          "query":    "popularity",
          "fields": [ "title", "content" ]
        }
      },
      "field_value_factor": {
        "field":    "votes",
        "modifier": "log1p",
        "factor":   0.1
      },
      "boost_mode": "sum",
      "max_boost":  1.5 <a id="CO116-1"></a><i class="conum" data-value="1"></i>
    }
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO116-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>无论 <code class="literal">field_value_factor</code> 函数的结果如何，最终结果都不会大于 <code class="literal">1.5</code> 。</p>
</td>
</tr>
</table>
</div>
<div class="note admon">
<div class="icon"></div>
<div class="admon_content">
<p><code class="literal">max_boost</code> 只对函数的结果进行限制，不会对最终评分 <code class="literal">_score</code> 产生直接影响。</p>
</div>
</div>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="function-score-query.html">« function_score 查询</a>
</span>
<span class="next">
<a href="function-score-filters.html">过滤集提升权重 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>